<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>【案例】建议留言板</title>
    <style>

    </style>
</head>
<body>
   <textarea name="" id=""></textarea>
    <button>发布</button>
    <ul></ul>
</body>
<script>
    //1.获取元素
    var btn = document.querySelector('button');
    var text = document.querySelector('textarea');
    var ul = document.querySelector('ul');
    //2.注册事件
    btn.onclick = function (){
        if(text.value == ''){
            alert('您没有输入的内容');
            return false;
        }else{
            //1.创建元素
            var li = document.createElement('li');
            li.innerHTML = text.value + '<a href="javascript:;">删除</a>'
            //2.添加元素
            ul.insertBefore(li,ul.children[0]);
            var as = document.querySelectorAll('a');
            for(var i = 0;i < as.length;i++){
                as[i].onclick = function (){
                    ul.removeChild(this.parentNode);
                };
            }
        }
    };
</script>
</html>